import '@umijs/max';
import { message, Modal } from 'antd';
import React from 'react';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { doPictureReviewUsingPost } from '@/services/backend/pictureController';

interface Props {
  data?: API.PictureVO;
  visible: boolean;
  onSubmit?: () => void;
  onCancel?: () => void;
}

/**
 * 创建弹窗
 * @param props
 * @constructor
 */
const ReviewModal: React.FC<Props> = (props) => {
  const { data, visible, onCancel, onSubmit } = props;
  /**
   * 更新节点
   *
   * @param fields
   */
  const handleUpdate = async (fields: API.PictureEditRequest) => {
    const hide = message.loading('正在更新');
    try {
      await doPictureReviewUsingPost(fields);
      hide();
      message.success('更新成功');
      return true;
    } catch (error: any) {
      hide();
      message.error('更新失败，' + error.message);
      return false;
    }
  };
  const columns: ProColumns<API.PictureReviewRequest>[] = [
    {
      title: '审核状态',
      dataIndex: 'reviewStatus',
      valueType: 'select',
      hideInForm: false,
      valueEnum: {
        0: {
          text: '待审核',
          status: 'Processing',
        },
        1: {
          text: '通过',
          status: 'Success',
        },
        2: {
          text: '拒绝',
          status: 'Error',
        },
      },
    },
    {
      title: '审核信息',
      dataIndex: 'reviewMessage',
      valueType: 'text',
    },
  ];
  return (
    <Modal
      destroyOnClose
      title={'审核'}
      open={visible}
      footer={null}
      onCancel={() => {
        onCancel?.();
      }}
    >
      <ProTable
        type="form"
        columns={columns}
        form={{
          initialValues: data,
        }}
        onSubmit={async (values: API.PictureUpdateRequest) => {
          const success = await handleUpdate({
            ...values,
            id: data?.id as any,
          });
          if (success) {
            onSubmit?.();
          }
        }}
      />
    </Modal>
  );
};
export default ReviewModal;
